I want to visualize the data categories, but I can't get what I want with the code below. When I want to divide the data into categories, it evaluates it as a whole. I can't get the chart categorized. What am I doing wrong?
library(shiny)
library(ggplot2)
ui <- fluidPage(
titlePanel("shiny demo"),
sidebarLayout(
sidebarPanel(
fileInput(inputId = "file",
label = "choose file",
multiple = F),
selectInput("p","variable1",
choices = c("sex","smoke","happines","city","work")),
selectInput("q", "variable2",
choices = c("hopefulnes","happines")))
,
mainPanel(
tabsetPanel(
tabPanel(title = "Data",tableOutput("data_out")),
tabPanel(title = "Bar",plotOutput("barp"))
)
)
)
)
server <- function(input,output,session) {
data <- reactive({
req(input$file)
df=read.csv(input$file$datapath,sep = ";",header = T)
})
output$data_out <- renderTable({
data()
})
output$barp <- renderPlot({
ggplot(data(), aes(input$p, ..count..)) + geom_bar(aes(fill = input$q), position = "dodge")
})
}
shinyApp(ui,server)
Data;
id sex smoke happines hopefulness work city
1 man yes very happy very hopeful yes A
2 man no very happy very hopeful yes A
3 man no unstable not hopeful no C
4 woman no unstable not hopeful yes A
5 woman no unstable not hopeful yes B
6 man yes very happy hopeful yes C
7 woman yes happy unstable no D
8 man yes not happy not hopeful yes A
9 woman no not happy unstable yes B
10 man no very happy very hopeful yes D
error code;
Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.
thank for help
selectInput()
is setup to output a string, whereas aes()
is setup to expect a named column. If you select "smoke" from input$p
and "happiness" from input$q
, this gets fed into the function as "smoke"
and "happiness"
, and not smoke
and happiness
. Consequently, it's going to plot as if you sent this function:
ggplot(df, aes("smoke", ..count..)) +
geom_bar(aes(fill = "happiness"), position = "dodge")
To handle the string output from selectInput()
, you should use aes_string()
in place of aes()
. You'll need to change ..count..
to "..count.."
, but it will work as if you asked to evaluate the following code chunk:
ggplot(df, aes_string("smoke", "..count..")) +
geom_bar(aes_string(fill = "happiness"), position = "dodge")
The other way you can do this is to maintain using aes()
, but just evaluate the string from input$p
and input$q
as a variable name. You can use get()
to do that normally, so I believe it will also work in your app. You don't have to change ..count..
to "..count.."
in this case:
# yields the same plot as above
ggplot(df, aes(get("smoke"), ..count..)) +
geom_bar(aes(fill = get("happiness")), position = "dodge")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.