Reputation: 797
The following code can be copied and pasted into RStudio and run:
salary <- c(45434,54834, 685485)
name <- c("john smith", "willy mcghee", "john paul stevens")
df <- data.frame(salary, name)
library(shiny)
library(dplyr)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Title
titlePanel("title"),
# this is the sidebar on the left when you run
sidebarLayout(
sidebarPanel(
# selection of names
selectInput("people",
label = "select the investor",
choices = c("john smith", "willy mcghee", "john paul stevens"))
),
mainPanel(
helpText("main panel text"),
textOutput("textsummary")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
vals <- reactiveValues()
observe({
vals$people <- input$people
vals$famousNetWorth <- df %>% filter(name == input$people)
} )
output$textsummary <- renderText({
paste(" the salary for ",vals$people, " is ", vals$famousNetWorth)
})
}
# Run the application
shinyApp(ui = ui, server = server)
This is the output I get:
Question
Why does the output print twice (highlighted in yellow)? The problem definitely stems from trying to select the value from the salary
column in my df
to match the user selected 'name',except I'm struggling to find a better way to reactivity pick the salary that matches with the input of name
. The next picture is hard coded-- this is what I want it to look like:
Upvotes: 1
Views: 1481
Reputation: 887118
This occurred (as @greg L mentioned in the comments) the output of the filter
step is still a data.frame
with two columns as in the original 'df'.
df %>%
filter(name == "john smith")
# salary name
#1 45434 john smith
Using paste
, it goes through each column and paste
the 'salary:' string with the column value
paste("salary:", df %>%
filter(name == "john smith"))
#[1] "salary: 45434" "salary: 2"
If we look at the second case, it looks perplexing, but check out the str
of the 'df'
str(df)
#'data.frame': 3 obs. of 2 variables:
# $ salary: num 45434 54834 685485
# $ name : Factor w/ 3 levels "john paul stevens",..: 2 3 1
As 'name' is factor
class, the storage model is integer
df$name
#[1] john smith willy mcghee john paul stevens
# Levels: john paul stevens john smith willy mcghee
Or check the levels
levels(df$name)
#[1] "john paul stevens" "john smith" "willy mcghee"
"john smith" is second level
df %>%
filter(name == "john smith") %>%
pull(name) %>%
as.integer
#[1] 2
While paste
ing the factor
got coerced to the integer storage mode
We need to pull
the 'salary' column out of that to get a vector
i.e.
vals$famousNetWorth <- df %>%
filter(name == input$people) %>%
pull(salary)
-full code
salary <- c(45434,54834, 685485)
name <- c("john smith", "willy mcghee", "john paul stevens")
df <- data.frame(salary, name)
library(shiny)
library(dplyr)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Title
titlePanel("title"),
# this is the sidebar on the left when you run
sidebarLayout(
sidebarPanel(
# selection of names
selectInput("people",
label = "select the investor",
choices = c("john smith", "willy mcghee", "john paul stevens"))
),
mainPanel(
helpText("main panel text"),
textOutput("textsummary")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
vals <- reactiveValues()
observe({
vals$people <- input$people
vals$famousNetWorth <- df %>%
filter(name == input$people) %>%
pull(salary)
} )
output$textsummary <- renderText({
paste(" the salary for ",vals$people, " is ", vals$famousNetWorth)
})
}
# Run the application
shinyApp(ui = ui, server = server)
-output
Upvotes: 3