[英]Creating a scatter plot using ggvis in a shiny app
我正在嘗試創建一個簡單的應用程序,它執行以下操作:
但是我無法創建繪圖 - 應該出現繪圖的 tabPanel 是空白的。 控制台中沒有任何錯誤或警告消息。 不知道代碼有什么問題。
這是代碼:
# Define UI for application
ui <- fluidPage(
# Application title
titlePanel("App"),
sidebarLayout(
sidebarPanel(
fileInput(inputId = "inputCrossTab"
, label = "Select the input sheet"
, multiple = FALSE
, accept = c('.csv')
)
),
# Show a table and plot
mainPanel(
tabsetPanel(id = "tabset"
, type = 'pills'
, tabPanel(title = "Table"
, DT::dataTableOutput('table')
)
, tabPanel(title = "Charts"
,ggvisOutput("plot")
)
)
)
)
)
# Define server logic required
server <- function(input, output) {
# Import csv data as a reactive
dat <- reactive({
req(input$inputCrossTab$datapath)
dat_tab <- data.table::fread( file = input$inputCrossTab$datapath
, header = TRUE)
})
# Render imported data as table
output$table <- DT::renderDataTable({
dat()
})
# Plot the table as a scatter plot
plot <- reactive({
dat()%>%
ggvis::ggvis(~Total.x,~Total.y)%>%
layer_points(fill:="red")%>%
bind_shiny("plot")
})
}
# Run the application
shinyApp(ui = ui, server = server)
這是我正在導入的 csv 文件(它只是一個 5 行 3 列的文件......一些隨機組成的數據)。
X1,Total.x,Total.y
ncksncnxzc,0.8338719625,0.0163952762
xsmkslaxmkaslx,0.5867098735,0.2033673932
njasdnsa,0.3586965341,0.8281010715
sadlasdl;,0.060212096,0.1735624054
nsakksad,0.7281606887,0.3851430044
還有一件事,為了可重復的示例,我在這里導入了一個 csv。 在我的實際代碼中,我正在導入一個 csv 文件並進行一些轉換並創建一個像給定的 csv 數據一樣的反應數據表。
請幫我解決這個問題。
謝謝,阿努普
您的服務器代碼中只有一個很小的錯誤。 在分配反應時,您必須添加“output$plot”而不僅僅是“plot”。 這是更正后的代碼。
library(ggvis)
ui <- fluidPage(
# Application title
titlePanel("App"),
sidebarLayout(
sidebarPanel(
fileInput(inputId = "inputCrossTab"
, label = "Select the input sheet"
, multiple = FALSE
, accept = c('.csv')
)
),
# Show a table and plot
mainPanel(
tabsetPanel(id = "tabset"
, type = 'pills'
, tabPanel(title = "Table"
, DT::dataTableOutput('table')
)
, tabPanel(title = "Charts"
,ggvisOutput("plot")
)
)
)
)
)
# Define server logic required
server <- function(input, output) {
# Import csv data as a reactive
dat <- reactive({
req(input$inputCrossTab$datapath)
dat_tab <- data.table::fread( file = input$inputCrossTab$datapath
, header = TRUE)
})
# Render imported data as table
output$table <- DT::renderDataTable({
dat()
})
# Plot the table as a scatter plot
output$plot <- reactive({
dat()%>%
ggvis::ggvis(~Total.x,~Total.y)%>%
layer_points(fill:="red")%>%
bind_shiny("plot")
})
}
# Run the application
shinyApp(ui = ui, server = server)```
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.