csvファイル入力のさまざまなグラフを出力するRプログラムを設計しています。プログラムの開発には Rstudio Shiny と ggplot2 を使用しています。
私の問題は、アルファベット順ではなく年代順に日付を並べることです(明らかにデフォルトです)。このコードを例として使用しましょう (私のコードは少し異なりますが、これは以前に私を助けてくれた人からのコードです):
関連記事: ggplot rshiny プログラムのグラフ形式を変更できません。バグを見つけてください。
R での月の並べ替え ノミナル変数をどのように順序付けますか。例えばRの月?
サーバー.R
library(shiny)
library(datasets)
library(ggplot2)
X <- read.csv(file.choose())
# Define server logic required to summarize and view the selected dataset
shinyServer(function(input, output) {
output$opt.x <- renderUI({
selectInput("xcolumn", "X column to Plot",
names(Y())
)
})
output$opt.y <- renderUI({
selectInput("ycolumn", "Y Column",
names(Y()))
})
# Generate a summary of the dataset
output$summary <- renderPrint({
dataset <- X
summary(dataset)
})
# Show the first "n" observations
output$view <- renderTable({
head(X, n = input$obs)
})
createPlot <- function(df, colx, coly) {
p <- ggplot(data=df, aes(x=df[,colx],y=df[,coly]), environment = environment()) #+ geom_line(aes(x=df[,colx],y=df[,coly], group=colx))
p <- p + geom_line(aes(group=colx))
p <- p + xlab(names(df)[colx]) + ylab(names(df)[coly])
}
Y <- reactive({
X
})
# create a basic plot
output$plotBasic <- reactivePlot(function() {
df <- Y()
print(createPlot(df, colx=input$xcolumn, coly=input$ycolumn))
})
})
ui.R
library(shiny)
# Define UI for dataset viewer application
shinyUI(pageWithSidebar(
# Application title
headerPanel("My app!"),
# Sidebar with controls to select a dataset and specify the number
# of observations to view
sidebarPanel(
numericInput("obs", "Number of observations to view:", 13),
uiOutput("opt.x"), #dynamic UI
uiOutput("opt.y") #value comes from Server.R
),
# Show a summary of the dataset and an HTML table with the requested
# number of observations
mainPanel(
tabsetPanel(
tabPanel("Table", tableOutput("view")),
tabPanel("BasicGraph", plotOutput("plotBasic"))
)
)
))
これは、既知のリストから開始した場合は factor または as.Date 関数を使用して簡単に処理できますが、ここでは入力を取り込んでおり (形式は mm-yyyy であると想定できます)、設定方法がわかりません。 x 変数データの列を変数に。これは、ユーザーがインポートされたデータの任意の列を X 列として選択できるためです。