R:使用shine创建一个应用程序来绘制csv文件



我正试图在R中创建一个闪亮的应用程序,该应用程序使用我拥有的CSV文件,但在尝试发布它时一直遇到错误。这是我的代码:`

library(shiny)
library(plotly)
library(dplyr)
library(ggplot2)
library(DT)
# Define UI for application that draws a histogram
ui <- fluidPage(# Application title
titlePanel("Hello"),



# Show a plot of the generated distribution
mainPanel(fig))

server <- function(input, output) {
output$table <- renderDT(investors)
output$fig <- renderPlot({
#
count_distinct_investors = investors %>% group_by(Marital_Status) %>% summarize(count =
                      n())

p = ggplot(count_distinct_investors,
aes(
x = reorder(Marital_Status,-count),
y = count,
fill = Marital_Status
)) + geom_bar(stat = "identity") +
coord_flip() + labs(title = "investors Count") + labs(x = "Marital_Status")

fig = ggplotly(p)
fig



})
}
# Run the application
shinyApp(ui = ui, server = server)

`

我已经预先加载到名为投资者的环境中,并有一个名为Marital_Status的专栏。这是我在尝试发布应用程序时遇到的错误:"应用程序无法启动:

exited unexpectedly with code 1
Loading required package: ggplot2
Attaching package: ‘plotly’
The following object is masked from ‘package:ggplot2’:
last_plot
The following object is masked from ‘package:stats’:
filter
The following object is masked from ‘package:graphics’:
layout

Attaching package: ‘dplyr’
The following objects are masked from ‘package:stats’:
filter, lag
The following objects are masked from ‘package:base’:
intersect, setdiff, setequal, union

Attaching package: ‘DT’
The following objects are masked from ‘package:shiny’:
dataTableOutput, renderDataTable
Error in value[[3L]](cond) : object 'fig' not found
Calls: local ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous>
Execution halted`

如果您希望plotly成为您的输出,您需要记住以下内容:

  1. 在服务器中:output$fig <- renderPlotly而不是output$fig <- renderPlot
  2. 在UI中:mainPanel(plotlyOutput("fig"))而不是mainPanel(fig))
  3. 您不必将绘图指定给对象,只需在末尾保留ggplotly(p)即可

你可以试试这个代码看看它是如何工作的:

library(shiny)
library(ggplot2)
library(ggthemes)
library(plotly)
ui <- fluidPage(  
titlePanel("Plotly"),
sidebarLayout(
sidebarPanel(),
mainPanel(
plotlyOutput("plot2"))
))
server <- function(input, output) {

output$plot2 <- renderPlotly({
ggplotly(
ggplot(data = mtcars, aes(x = disp, y = cyl)) + 
geom_smooth(method = lm, formula = y~x) + 
geom_point() + 
theme_gdocs())
})
}
shinyApp(ui, server)

最新更新